package com.innovatio.validator.handlers;

import com.innovatio.validator.annotations.ValidateNotNull;
import com.innovatio.validator.ValidationErrors;
import com.innovatio.validator.Validable;
import com.innovatio.validator.exceptions.ValidatorException;
import com.innovatio.utils.general.CommonsUtils;

/**
 * This code is property of Innovatio Software Solutions, Inc.
 * User: Edxe
 * Date: Jul 6, 2009
 * Time: 5:43:35 AM
 */
public class ValidateNotNullHandler implements ValidationHandler<ValidateNotNull> {

    @Override
    public ValidationErrors.Error validate(ValidateNotNull annotation, Validable container, String fieldName, String value) throws ValidatorException {

        if (annotation.addOnEmptyConstraint()) {
            if (value == null || CommonsUtils.EMPTY.equals(value))
                return new ValidationErrors.Error(fieldName, annotation.i18nBundleKey());
        } else if (value == null)
            return new ValidationErrors.Error(fieldName, annotation.i18nBundleKey());
        return null;
    }
}
